/*
 * The X-Minou Projet
 * X-Minou is a text, irc like chat program for
 * local users of Unix system
 *
 * Author :  RaX| <raxxx.fr@gmail.com>
 *
 * Xminou in a free software released as-is.
 */

#include <iomanip>
#include <sstream>

#include <sys/stat.h>

#include "xminou_write.hh"
#include "xminou.hh"

WriteToFile::WriteToFile ()
{

  m_file.open (SOCKET_FILE, std::fstream::app | std::fstream::ate);

  chmod (SOCKET_FILE, S_IWUSR
    | S_IRUSR
    | S_IWOTH
    | S_IROTH
    | S_IWGRP
    | S_IRGRP);

}

void
WriteToFile::send_action (std::string str)
{

  m_file
#ifdef _USE_COLOR
   << "\e[0;32m"
#endif
    << get_current_date ()
    << " * "
    << whoami ()
    << " "
    << str
#ifdef _USE_COLOR
    <<"\e[0m"
#endif
    << std::endl;
}

void
WriteToFile::send_quit_action (std::string str)
{

  std::string act = "has quit";

  if (!str.empty ())
    act += " ( " + str + " )";

  send_action (act);

}

void
WriteToFile::send_message (std::string str)
{

  if (str.empty ())
    return;

  m_file << get_current_date ()
#ifdef _USE_COLOR
    << "\e[0;31m"
#endif
    << " <"
#ifdef _USE_COLOR
    << "\e[1;34m"
#endif
    << whoami ()
#ifdef _USE_COLOR
    << "\e[0;31m"
#endif
    << "> "
#ifdef _USE_COLOR
    << "\e[0m"
#endif
    << str
    << std::endl;

}

std::string
WriteToFile::get_current_date (void)
{

  time_t raw_time;
  struct tm *tm;
  
  std::stringstream s_str;

  time (&raw_time);
  tm = localtime (&raw_time);

  s_str
#ifdef _USE_COLOR
    << "\e[0;33m"
#endif
    << "("
    << std::setw (2) << std::setfill('0')
    << tm->tm_hour
    << ":"
    << std::setw (2) << std::setfill('0')
    << tm->tm_min
    << ":"
    << std::setw (2) << std::setfill('0')
    << tm->tm_sec
#ifdef _USE_COLOR
    << ")\e[0m";
#else
    << ")";
#endif

  return s_str.str ();

}

void
WriteToFile::easter_egg_bugs (std::string str)
{

  m_file
#ifdef _USE_COLOR
    << "\e[1;34m"
#endif
    << " " << std::endl
    << "        /\\  /\\" << std::endl
    << "       |  \\ \\ \\`\\" << std::endl
    << "       |   \\ \\  \\`\\" << std::endl
    << "        \\   \\ \\   |" << std::endl
    << "         \\   \\|   |" << std::endl
    << "          \\   \\   |" << std::endl
    << "           \\  |\\  |" << std::endl
    << "         .-' .--. |" << std::endl
    << "       /^\\  ' .-. (" << std::endl
    << "       /\\    /   \\ \\" << std::endl
    << "       | |   |   |  |" << std::endl
    << "    .'\\`|O|/  |__O|.'--." << std::endl
    << "   {   '/___,          \\" << std::endl
    << "   \\`. ';(_Y_) \\`-'/    .'" << std::endl
    << "     \\  \\\\_| .'  _.-'" << std::endl
    << "      \\`'.\\`._.'.''\\`" << "  " << str << std::endl
    << "         \\`-..'" << std::endl
    << " " << std::endl
#ifdef _USE_COLOR
    << "\e[0m"
#endif
    << std::endl;
}

void
WriteToFile::easter_egg_minou (std::string str)
{

  m_file
#ifdef _USE_COLOR
    << "\e[1;34m"
#endif
    << std::endl
    << "(\\___/)" << std::endl
    << "(=*.*=)" << std::endl
    << "(\")__(\") < "
    << str
    << " >"
    << std::endl
    << " "
#ifdef _USE_COLOR
    << "\e[0m"
#endif
    << std::endl;

}

void
WriteToFile::easter_egg_homer (std::string str)
{

  m_file
#ifdef _USE_COLOR
    << "\e[1;34m"
#endif
    << "   ___  _____" << std::endl
    << " .'/,-Y\"     \"~-." << std::endl
    << " l.Y             ^." << std::endl
    << " /\\               _\\_" << std::endl
    << "i            ___/\"   \"\"" << std::endl
    << "|          /\"   \"\\   o !" << std::endl
    << "l         ]     o !__./" << std::endl
    << " \\ _  _    \\.___./    \"~\\" << std::endl
    << "  X \\/ \\            ___./" << std::endl
    << " ( \\ ___.   _..--~~\"   ~\\`-." << std::endl
    << "  \\` Z,--   /               \\" << std::endl
    << "    \\__.  (   /       ______)" << std::endl
    << "      \\   l  /-----~~\" \\/"  << " " << str << std::endl
    << "       Y   \\          /" << std::endl
    << "       |    \"x______.^" << std::endl
    << "       |           \\" << std::endl
    << "       j            Y" << std::endl
    << " " << std::endl
#ifdef _USE_COLOR
    << "\e[0m"
#endif
    << std::endl;

}

WriteToFile::~WriteToFile (void)
{

  m_file.close ();

}

